/*++****************************************************************************
*
*	Name: a2d.c
*   
*	Description:	Function to initialize and read analog inputs
*
*	Revision History:
*	Date	Who	Comments	
*	------	---	---------------------------------------------------------------
*	100303	DHT	Created
****************************************************************************--*/
#define USE_OR_MASKS
#include <p18cxxx.h>
#include <adc.h>


void A2D_init( char maxA2Dchan )  // highest analog channel number
{
	// turn off the adc module
	ADCON0bits.ADON = 0;
	
	// if maxA2Dchan is negative then that means 
	// no analog channels
	if (maxA2Dchan < 0)
	{
		ADCON1 = 0b1111;	 // turn all analog pins into digital
		return;
	}
		
	if (maxA2Dchan > 12)
		maxA2Dchan = 12;
		
	// setup analog channels
	ADCON1 = 14 - maxA2Dchan;
	
	// setup adc module for right justified, 4 TAD, and Fosc/4
	ADCON2 = 0b10010100;
	
	// turn on the adc module
	ADCON0bits.ADON = 1;
}

	
int A2D_read(unsigned char chan)
{
	// select adc channel and begin conversion
	ADCON0 = ((chan & 0x0f) << 2) | 0b11;
	
	// wait for conversion to finish
	while (ADCON0bits.GO);
	
	return ReadADC();
}


	